package com.fs.service;

import com.fs.bean.MonitorFalconTrend;
import com.fs.constant.SegmentEnum;
import com.fs.dao.MonitorFalconDao;
import com.fs.domain.DbInfo;
import com.fs.domain.MonitorFalcon;
import com.fs.domain.helper.MonitorFalconHelper;
import com.fs.param.MonitorFalconDynamicParam;
import com.fs.vo.MonitorFalconVo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by fengshuai on 17/5/18.
 */
@Service
public class MonitorFalconService {
    protected static Logger logger = Logger.getLogger(MonitorFalconService.class);

    @Autowired
    private MonitorFalconDao monitorFalconDao;

    @Autowired
    private DbInfoService dbInfoService;

    public List<MonitorFalconVo> batchGetByDay(int day) {
        List<MonitorFalcon> domains = this.monitorFalconDao.batchGetByDay(day);
        List<MonitorFalconVo> vos = MonitorFalconHelper.convertToVos(domains);
        Map<Integer, DbInfo> map = this.dbInfoService.getAllMap();
        for (MonitorFalconVo vo : vos) {
            vo.setDbName(map.get(vo.getDbId()).getName());
            vo.setSegmentComment(SegmentEnum.getByType(vo.getSegment()).getComment());
        }
        return vos;
    }

    public List<MonitorFalconVo> batchGetByDynamicParam(MonitorFalconDynamicParam param) {
        List<MonitorFalcon> domains = this.monitorFalconDao.batchGetByDynamicParam(param.getDay(), param.getDbId(), param.getSegment());
        Map<String, MonitorFalcon> dbIdToDomains = new HashMap<>();
        List<MonitorFalcon> targetDomains = new ArrayList<>();
        for (MonitorFalcon domain : domains) {
            dbIdToDomains.put(domain.getDb_id() + "-" + domain.getSegment(), domain);
        }
        if (param.getSegment() != 0) {
            for (String key : dbIdToDomains.keySet()) {
                int segment = Integer.parseInt(key.split("-")[1]);
                if (param.getSegment() == segment) {
                    targetDomains.add(dbIdToDomains.get(key));
                }
            }
        } else {
            targetDomains.addAll(dbIdToDomains.values());
        }
        List<MonitorFalconVo> vos = MonitorFalconHelper.convertToVos(targetDomains);
        Map<Integer, DbInfo> map = this.dbInfoService.getAllMap();
        for (MonitorFalconVo vo : vos) {
            vo.setDbName(map.get(vo.getDbId()).getName());
            vo.setDbType(map.get(vo.getDbId()).getType());
            vo.setSegmentComment(SegmentEnum.getByType(vo.getSegment()).getComment());
        }
        Collections.sort(vos, new Comparator<MonitorFalconVo>() {
            @Override
            public int compare(MonitorFalconVo o1, MonitorFalconVo o2) {
                if (Integer.compare(o1.getDbId(), o2.getDbId()) == 0) {
                    return Integer.compare(o1.getSegment(), o2.getSegment());
                } else {
                    return Integer.compare(o1.getDbId(), o2.getDbId());
                }
            }
        });
        return vos;
    }

    public List<MonitorFalconTrend> batchGetMaxQpsByDayAndDbIdsV2(List<Integer> dbIds, int startDay, int endDay, int segment) {
        //只能分两步了
        List<Long> ids = this.monitorFalconDao.batchGetLatestIdsByDayAndDbIds(startDay, endDay, dbIds, segment);
        return this.monitorFalconDao.batchGetByIds(ids);
    }
}
